package com.example.yuekaoceshi;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yuekaoceshi.http.TypeApi;
import com.example.yuekaoceshi.moudle.Moudle;
import com.example.yuekaoceshi.presenter.Presenter;
import com.example.yuekaoceshi.view.Main_view;
import com.umeng.socialize.UMShareAPI;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements Main_view {

    @BindView(R.id.login_qq)
    ImageView mLoginQq;
    @BindView(R.id.login_tv1)
    EditText mLoginTv1;
    @BindView(R.id.login_tv2)
    EditText mLoginTv2;
    @BindView(R.id.login_bt)
    Button mLoginBt;
    Presenter mPresenter;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode,resultCode,data);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mPresenter = new Presenter(new Moudle(), this);
    }
    @OnClick({R.id.login_qq, R.id.login_bt})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.login_qq:
            mPresenter.getdata(null,null,this,TypeApi.LOGIN);
                break;
            case R.id.login_bt:
                if (!mLoginTv1.getText().toString().isEmpty()&&!mLoginTv2.getText().toString().isEmpty()){
                    Intent intent = new Intent(this,MainActivity2.class);
                    startActivity(intent);
                }else {
                    Toast.makeText(this, "账号或密码不能为空", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    @Override
    public void showData(Object o, TypeApi typeApi) {
        Map<String,Object> map= (Map<String, Object>) o;
        for (String key : map.keySet()) {
            Log.e("liangxq", "key=" + key + "value=" + map.get(key));
        }
        String name = (String) map.get("name");
        String  expiration = (String) map.get("expiration");
        mLoginTv1.setText(name);
        mLoginTv2.setText(expiration);
    }

    @Override
    public void showError(String error) {
        Log.e("duan", "showError: "+error);
    }
}
